﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCRUI.OCRBatchManager
{
    public class Document : BoundedTextBase<Line>
    {
        /// <summary>
        /// Path to the orinial Tif image of text
        /// </summary>
        public string TifImagePath { get; set; }
        /// <summary>
        /// Path to the OCR'd text file
        /// </summary>
        public string TextFilePath { get; set; }
        public new void Parse(List<string> document)
        {
            this.ChildItems = new List<Line>();
            List<int[]> LineRanges = GetTextLineBounds(document);
            foreach (int[] range in LineRanges)
            {
                Line l = new Line(){ BoundTextStartPos = range[0], BoundTextEndPos = range[1]};
                l.Parse(document);
                this.ChildItems.Add(l);
            }
            
            
        }
        /// <summary>
        /// Determines where each line starts and ends.
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static List<int[]> GetTextLineBounds(List<string> document)
        {
            int lnStart = 0;
            List<int[]> Lines = new List<int[]>();
            for (int i = 0; i < document.Count; i++)
            {
                if (document[i].Trim() == "<nl>" || document[i].Trim() == "<para>")
                {
                    int[] lnRange = new int[2] { lnStart, i };
                    Lines.Add(lnRange);
                    lnStart =i =  i +2;
                }
            }
            return Lines;
            

        }
        
    }
}
